package com.simpleframe.security;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.simpleframe.exception.SimpleframeException;

public class AuthorityContext {
	/** 用于缓存角色 **/
	private static Map<Long, Role> rolesMap = new HashMap<Long, Role>();
	/** 用于缓存所有权限 **/
	private static List<Module> modules = new ArrayList<Module>();
//	/** 用于缓存所有权限 **/
//	private static Map<Long, String> actionIdActionUrlMap = new HashMap<Long, String>();
//	/** 用于缓存所有权限用于后台修改或删除权限时用 **/
	private static Map<String, Action> actions = new HashMap<String, Action>(); 
	
	
	/**
	 * 验证用户对操作uri是否存在操作权限
	 * @param roleId
	 * @param uri
	 * @return
	 */
   public static boolean verifyOperate(List<Long> roleIds,String uri) {
		if(rolesMap.size()==0){
			System.out.println("角色列表不存在，不开启权限");
			return true;
		}
		if(roleIds == null || roleIds.size() == 0) 
			return false;
		for(Long roleId : roleIds) {
			if(rolesMap.containsKey(roleId)){
				Role role = rolesMap.get(roleId);
				if(role.verifyOperate(uri))
					return true;
			}
		}
		
		return false;
   }
   
   
//   /**
//	 * 验证用户对模块module是否存在权限
//	 * @param roleId
//	 * @param uri
//	 * @return
//	 */
//  public static boolean verifyModule(List<Long> roleIds, String moduleName) {
//		if(rolesMap.size()==0){
//			System.out.println("角色列表不存在，不开启权限");
//			return true;
//		}
//		if(roleIds == null || roleIds.size() == 0) 
//			return false;
//		for(Long roleId : roleIds) {
//			if(rolesMap.containsKey(roleId)){
//				Role role = rolesMap.get(roleId);
//				if(role.verifyModule(moduleName)) 
//					return true;
//			}
//		}
//		
//		return false;
//  } 
   
   
   public static boolean isProtectResource(String uri) {
	   if(actions.containsKey(uri)) 
		   return true;
	   return false;
   }
   
   public static Action getAction(String id){
	   for (Module module : modules) {
		   for (Action action : module.getActions()) {
			   if(action.getId().equals(id)){
				   return action;
			   }
		   }
	   }
	   return null;
   }
   
   public static void addModule(Module module){
	   modules.add(module);
	   for (Action action : module.getActions()) {
		   actions.put(action.getUrl(), action);
	   }
   }
   
   public static List<Module> getModules(){
	   return modules;
   }
   
   /**
    * 这样的做法，如果多人一起点击会出问题的
    * @param roleId
    * @return
    */
   public static List<Module> getModules(Long roleId){
	   List<Module> roleModules = new ArrayList<Module>();
	   Role role = getRole(roleId);
	   roleModules.addAll(modules);
	   for (Module module : roleModules) {
		   for (Action action : module.getActions()) {
			   action.setSelect("");
			   if(role.verifyOperate(action.getUrl())){
				   action.setSelect("checked");
			   }
		   }
	   }
	   return roleModules;
   }
	
	public static Role getRole(Long roleId) {
		if(roleId == null) 
			throw new SimpleframeException("角色ID不能为空!");
		return rolesMap.get(roleId);
	}
	
	public static List<Role> getRoles(){
		List<Role> list = new ArrayList<Role>();
		for (Long key : rolesMap.keySet()) {
			list.add(rolesMap.get(key));
		}
		return list;
	}
	
	public static List<Role> getRoles(List<Long> roleIds){
		List<Role> list = new ArrayList<Role>();
		for (Long key : rolesMap.keySet()) {
			Role role = rolesMap.get(key);
			role.setSelect("");
			if(roleIds.contains(role.getId())){
				role.setSelect("checked");
			}
			list.add(rolesMap.get(key));
		}
		return list;
	}
	
	public static void addRole(Role role) {
		if(role == null || role.getId() == null) 
			throw new SimpleframeException("角色不能为空或角色ID不能为空!");
		rolesMap.put(role.getId(), role);
	}
	
	public static void deleteRole(Long roleId) {
		if(roleId == null) 
			throw new SimpleframeException("角色ID不能为空!");
		rolesMap.remove(roleId);
	}
	
//	public static boolean isActionUrlExist(String actionUrl) {
//		if(actionUrlActionIdMap.containsKey(actionUrl)) 
//			return true;
//		return false;
//	}
//	
//	public static void addAction(Long actionId, String actionUrl) {
//		actionIdActionUrlMap.put(actionId, actionUrl);
//		actionUrlActionIdMap.put(actionUrl, actionId);
//	}
//	
//	public static void deleteAction(Long actionId, String actionUrl) {
//		actionIdActionUrlMap.remove(actionId);
//		actionUrlActionIdMap.remove(actionUrl);
//	}
//	
//	public static void updateAction(Long actionId, String actionUrl) {
//		if(!actionIdActionUrlMap.containsKey(actionId)) 
//			return;
//		String oldActionUrl = actionIdActionUrlMap.get(actionId);
//		actionUrlActionIdMap.remove(oldActionUrl);
//		actionIdActionUrlMap.put(actionId, actionUrl);
//		actionUrlActionIdMap.put(actionUrl, actionId);
//	}
}
